package com.example.codereview;

import com.example.codereview.util.IdeaUtils;
import com.intellij.openapi.application.ApplicationManager;
import com.intellij.openapi.fileEditor.FileEditorManager;
import com.intellij.openapi.fileEditor.FileEditorManagerEvent;
import com.intellij.openapi.fileEditor.FileEditorManagerListener;
import com.intellij.openapi.project.Project;
import com.intellij.openapi.vfs.VirtualFile;
import org.jetbrains.annotations.NotNull;

public class MyFileOpenListener implements FileEditorManagerListener {
    @Override
    public void fileOpened(@NotNull FileEditorManager source, @NotNull VirtualFile file) {
        Project project = source.getProject();
        // 打开的是Java文件，执行你的处理逻辑
        IdeaUtils.processJavaFileOpening(project, file);
    }

    @Override
    public void selectionChanged(@NotNull FileEditorManagerEvent event) {
        VirtualFile newFile = event.getNewFile();
        Project project = event.getManager().getProject();
        // 打开的是Java文件，执行你的处理逻辑
        IdeaUtils.processJavaFileOpening(project, newFile);
    }


    public static void registerListener() {
        ApplicationManager.getApplication().getMessageBus().connect()
                .subscribe(FileEditorManagerListener.FILE_EDITOR_MANAGER, new MyFileOpenListener());
    }
}
